package com.logic.landseaserver.ws;

import java.util.List;

import javax.ws.rs.Consumes;
import javax.ws.rs.DELETE;
import javax.ws.rs.GET;
import javax.ws.rs.POST;
import javax.ws.rs.PUT;
import javax.ws.rs.Path;
import javax.ws.rs.PathParam;
import javax.ws.rs.Produces;
import javax.ws.rs.QueryParam;
import javax.ws.rs.core.MediaType;
import javax.ws.rs.core.Response;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;

import com.logic.common.enums.HttpCodeEnum;
import com.logic.common.exception.BusinessException;
import com.logic.common.util.CommonResult;
import com.logic.common.util.CommonResultMap;
import com.logic.common.util.QueryUtil;
import com.logic.landseaserver.domain.Room;
import com.logic.landseaserver.domain.RoomType;
import com.logic.landseaserver.domain.SystemCode;
import com.logic.landseaserver.service.IProjectAppartment;
import com.logic.landseaserver.ws.dto.RoomDetailDTO;
import com.logic.landseaserver.ws.dto.RoomTypeDTO;
import com.logic.landseaserver.ws.request.RoomDetailReq;
import com.logic.landseaserver.ws.request.RoomStatusListReq;
import com.logic.landseaserver.ws.request.RoomStatusStatisticReq;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;

/**
 * Created by Tony on 2017/4/12.
 */


@Path("/room")
@Api(value = "room", description = "Room Rest WebServices", position = 42)
public class RoomRest {

    @Autowired
    private IProjectAppartment projectAppartmentImpl;

    private Logger logger = LoggerFactory.getLogger(this.getClass());
    
    //根据 项目id 以及 查询条件 查询房屋列表
    @POST
    @Path("/query/{projectId}")
    @Produces(MediaType.APPLICATION_JSON)
    @Consumes(MediaType.APPLICATION_JSON)
    @ApiOperation(value = "根据项目id获取房间列表")
    public Response queryProjectAppartments(
            @ApiParam(value = "param", required = true) @PathParam("projectId") Integer projectId,
            QueryUtil queryUtil) {
        CommonResultMap map = new CommonResultMap();
        List<Object> projectAppartmentList = null;
        try {
            queryUtil.getPagingTool().calculateStartIndex();
            projectAppartmentList = projectAppartmentImpl.queryByPage(queryUtil, projectId);
            Integer totalCount = queryUtil.getPagingTool().getTotalNum();
            map.setData(projectAppartmentList);
            map.setResultTotalCount(totalCount);
            map.setResult(CommonResult.SUCCESS);
        } catch (Exception e) {
            logger.error("queryProjectAppartments error.", e);
            map.setResult(CommonResult.ERROR, e.getMessage());
        }
        return Response.status(HttpCodeEnum.OK.getCode()).entity(map).build();
    }

    //根据 项目id 以及 查询条件 查询房屋列表
    @POST
    @Path("/queryRoomList")
    @Produces(MediaType.APPLICATION_JSON)
    @Consumes(MediaType.APPLICATION_JSON)
    @ApiOperation(value = "获取所有房间列表")
    public Response queryRoomList(
            @ApiParam(value = "param", required = false)
                    QueryUtil queryUtil) {
        CommonResultMap map = new CommonResultMap();
        List<Object> projectAppartmentList = null;
        try {
            queryUtil.getPagingTool().calculateStartIndex();
            Integer projectId = null;
            projectAppartmentList = projectAppartmentImpl.queryByPage(queryUtil, projectId);
            Integer totalCount = queryUtil.getPagingTool().getTotalNum();
            map.setData(projectAppartmentList);
            map.setResultTotalCount(totalCount);
            map.setResult(CommonResult.SUCCESS);
        } catch (Exception e) {
            logger.error("queryRoomList error.", e);
            map.setResult(CommonResult.ERROR, e.getMessage());
        }
        return Response.status(HttpCodeEnum.OK.getCode()).entity(map).build();
    }

    //删除对应的房间
    @DELETE
    @Produces(MediaType.APPLICATION_JSON)
    @ApiOperation(value = "根据房间id删除对应房间（可以批量删除）", position = 12)
    public Response deleteProjectAppartment(
            @ApiParam(value = "param", required = true) @QueryParam("roomIds") String ids) {
        CommonResultMap map = new CommonResultMap();
        try {
            projectAppartmentImpl.delete(ids);
            map.setResult(CommonResult.SUCCESS);
        } catch (BusinessException e) {
            logger.error("deleteProjectAppartment error.", e);
            map.setResult(CommonResult.ERROR, e.getMessage());
        }
        return Response.status(HttpCodeEnum.OK.getCode()).entity(map).build();
    }

    //更新房间状态
    @PUT
    @Path("/updateStatus")
    @Produces(MediaType.APPLICATION_JSON)
    @Consumes(MediaType.APPLICATION_JSON)
    @ApiOperation(value = "更新房间状态")
    public Response updateProjectAppartmentStatus(
            @ApiParam(value = "param", required = true) @QueryParam("roomId") Integer id, @QueryParam("statusCode") String statusCode) {
        CommonResultMap map = new CommonResultMap();
        try {
            // ProjectAppartment projectAppartment =
            // ProjectAppartmentDTO.toProjectApartmentPOJO(projectAppartmentDTO);
            Room room = new Room();
            room.setId(id);
            room.setStatus(statusCode);
            if (statusCode.equals("LRA2")) {
                room.setAppendStatus("LRB1");
            } else {
                room.setAppendStatus("");
            }
            projectAppartmentImpl.updateStatus(room);

            map.setResult(CommonResult.SUCCESS, "Update status success");
        } catch (BusinessException e) {
            logger.error("updateProjectAppartmentStatus error.", e);
            map.setResult(CommonResult.ERROR, e.getMessage());
        } catch (Exception e) {
            logger.error("updateProjectAppartmentStatus error.", e);
            map.setResult(CommonResult.ERROR, e.getMessage());
        }
        return Response.status(HttpCodeEnum.OK.getCode()).entity(map).build();
    }

    //更新房间经营状态
    @PUT
    @Path("/updateAppendStatus")
    @Produces(MediaType.APPLICATION_JSON)
    @Consumes(MediaType.APPLICATION_JSON)
    @ApiOperation(value = "更新房间经营状态（append status）")
    public Response updateRoomAppendStatus(
            @ApiParam(value = "param", required = true) Room room) {
        CommonResultMap map = new CommonResultMap();
        try {
            // ProjectAppartment projectAppartment =
            // ProjectAppartmentDTO.toProjectApartmentPOJO(projectAppartmentDTO);
            projectAppartmentImpl.updateAppendStatus(room);
            map.setResult(CommonResult.SUCCESS, "Update append status success");
        } catch (BusinessException e) {
            logger.error("updateRoomAppendStatus error.", e);
            map.setResult(CommonResult.ERROR, e.getMessage());
        } catch (Exception e) {
            logger.error("updateRoomAppendStatus error.", e);
            map.setResult(CommonResult.ERROR, e.getMessage());
        }
        return Response.status(HttpCodeEnum.OK.getCode()).entity(map).build();
    }

    //增加房间
    @POST
    @Path("/addRoom")
    @Produces(MediaType.APPLICATION_JSON)
    @Consumes(MediaType.APPLICATION_JSON)
    @ApiOperation(value = "创建一个房间")
    public Response createProjectAppartment(
            @ApiParam(value = "param", required = true) RoomDetailDTO roomDetailDTO) {  //需要项目的编码
        CommonResultMap map = new CommonResultMap();
        try {
            Integer projectAppartmentId = projectAppartmentImpl.insertRoomDetail(roomDetailDTO);
            map.setData(projectAppartmentId);
            map.setResult(CommonResult.SUCCESS, "Create success");
        } catch (BusinessException e) {
            logger.error("createProjectAppartment error.", e);
            map.setResult(CommonResult.BUSINESS_EXCEPTION, e.getMessage());
        } catch (Exception e) {
            logger.error("createProjectAppartment error.", e);
            map.setResult(CommonResult.ERROR, e.getMessage());
        }
        return Response.status(HttpCodeEnum.OK.getCode()).entity(map).build();
    }

    //编辑房间
    @POST
    @Path("/editRoom")
    @Produces(MediaType.APPLICATION_JSON)
    @Consumes(MediaType.APPLICATION_JSON)
    @ApiOperation(value = "编辑房间")
    public Response editProjectAppartment(
            @ApiParam(value = "param", required = true) RoomDetailDTO roomDetailDTO) {
        CommonResultMap map = new CommonResultMap();
        try {
            Integer projectAppartmentId = projectAppartmentImpl.editRoomDetail(roomDetailDTO);
            map.setData(projectAppartmentId);
            map.setResult(CommonResult.SUCCESS, "Create success");
        } catch (BusinessException e) {
            logger.error("editProjectAppartment error.", e);
            map.setResult(CommonResult.ERROR, e.getMessage());
        } catch (Exception e) {
            logger.error("editProjectAppartment error.", e);
            map.setResult(CommonResult.ERROR, e.getMessage());
        }
        return Response.status(HttpCodeEnum.OK.getCode()).entity(map).build();
    }

    //房间详情
    @PUT
    @Produces(MediaType.APPLICATION_JSON)
    @ApiOperation(value = "获取房间详情", position = 12)
    public Response infoProjectAppartment(
            @ApiParam(value = "param", required = true) @QueryParam("roomId") int id) {
        CommonResultMap map = new CommonResultMap();
        RoomDetailReq obj = null;
        try {
            obj = projectAppartmentImpl.searchRoomInfo(id);
            map.setData(obj);
            map.setResult(CommonResult.SUCCESS);
        } catch (Exception e) {
            logger.error("infoProjectAppartment error.", e);
            map.setResult(CommonResult.ERROR, e.getMessage());
        }
        return Response.status(HttpCodeEnum.OK.getCode()).entity(map).build();
    }

    //获取房间状态
    @GET
    @Path("/roomStatusList")
    @Produces(MediaType.APPLICATION_JSON)
    @ApiOperation(value = "get room status", position = 12)
    public Response getRoomStatusList() {
        CommonResultMap map = new CommonResultMap();
        List<SystemCode> roomStatusList = null;
        try {
            roomStatusList = projectAppartmentImpl.searchRoomStatusList();
            map.setData(roomStatusList);
            map.setResult(CommonResult.SUCCESS);
        } catch (Exception e) {
            logger.error("getRoomStatusList error.", e);
            map.setResult(CommonResult.ERROR, e.getMessage());
        }
        return Response.status(HttpCodeEnum.OK.getCode()).entity(map).build();
    }

    //获取房间经营状态
    @GET
    @Path("/roomAppendStatusList")
    @Produces(MediaType.APPLICATION_JSON)
    @ApiOperation(value = "get room append status", position = 12)
    public Response getRoomAppendStatusList() {
        CommonResultMap map = new CommonResultMap();
        List<SystemCode> roomAppendStatusList = null;
        try {
            roomAppendStatusList = projectAppartmentImpl.searchRoomAppendStatusList();
            map.setData(roomAppendStatusList);
            map.setResult(CommonResult.SUCCESS);
        } catch (Exception e) {
            logger.error("getRoomAppendStatusList error.", e);
            map.setResult(CommonResult.ERROR, e.getMessage());
        }
        return Response.status(HttpCodeEnum.OK.getCode()).entity(map).build();
    }

    //获取户型
    @GET
    @Path("/roomTypeList")
    @Produces(MediaType.APPLICATION_JSON)
    @ApiOperation(value = "get room type", position = 12)
    public Response getRoomTypeList() {
        CommonResultMap map = new CommonResultMap();
        List<SystemCode> roomTypeList = null;
        try {
            roomTypeList = projectAppartmentImpl.searchRoomTypeList();
            map.setData(roomTypeList);
            map.setResult(CommonResult.SUCCESS);
        } catch (Exception e) {
            logger.error("getRoomTypeList error.", e);
            map.setResult(CommonResult.ERROR, e.getMessage());
        }
        return Response.status(HttpCodeEnum.OK.getCode()).entity(map).build();
    }


    //根据项目获取装修风格
    @PUT
    @Path("/getDecorateStyleByProjectId")
    @Produces(MediaType.APPLICATION_JSON)
    @ApiOperation(value = "根据项目id获取装修风格", position = 12)
    public Response getRoomTypeByProjectId(
            @ApiParam(value = "param", required = true) @QueryParam("projectId") int id) {
        CommonResultMap map = new CommonResultMap();
        List<SystemCode> obj = null;
        try {
            obj = projectAppartmentImpl.searchDecorateStyleByProjectID(id);
            map.setData(obj);
            map.setResult(CommonResult.SUCCESS);
        } catch (Exception e) {
            logger.error("getRoomTypeByProjectId error.", e);
            map.setResult(CommonResult.ERROR, e.getMessage());
        }
        return Response.status(HttpCodeEnum.OK.getCode()).entity(map).build();
    }


    //根据项目获取可用装修风格
    @PUT
    @Path("/getValideDecorateStyle")
    @Produces(MediaType.APPLICATION_JSON)
    @ApiOperation(value = "根据项目id获取可用的装修风格", position = 12)
    public Response getValideDecorateStyle(
            @ApiParam(value = "param", required = true) @QueryParam("projectId") int id) {
        CommonResultMap map = new CommonResultMap();
        List<SystemCode> obj = null;
        try {
            obj = projectAppartmentImpl.getValideDecorateStyle(id);
            map.setData(obj);
            map.setResult(CommonResult.SUCCESS);
        } catch (Exception e) {
            logger.error("getValideDecorateStyle error.", e);
            map.setResult(CommonResult.ERROR, e.getMessage());
        }
        return Response.status(HttpCodeEnum.OK.getCode()).entity(map).build();

    }


    @POST
    @Path("/getProjStylePic")
    @Produces(MediaType.APPLICATION_JSON)
    @Consumes(MediaType.APPLICATION_JSON)
    @ApiOperation(value = "根据id获取价格、设备、详情图片等信息")
    public Response getPicsByPrjIdAndStyle(
            @ApiParam(value = "param", required = true) @QueryParam("id") Integer id) {
        CommonResultMap map = new CommonResultMap();
        try {
            RoomType roomPicture = new RoomType();
            roomPicture = projectAppartmentImpl.getDecoratePic(id);
            map.setData(roomPicture);
            map.setResult(CommonResult.SUCCESS, "get picture id success");
        } catch (Exception e) {
            logger.error("getPicsByPrjIdAndStyle error.", e);
            map.setResult(CommonResult.ERROR, e.getMessage());
        }
        return Response.status(HttpCodeEnum.OK.getCode()).entity(map).build();
    }

    //删除项目图片信息
    @POST
    @Path("/deleteProjStylePic")
    @Produces(MediaType.APPLICATION_JSON)
    @Consumes(MediaType.APPLICATION_JSON)
    @ApiOperation(value = "根据项目id和房型，删除项目房型下的价格、设施、图片信息")
    public Response deletePictures(
            @ApiParam(value = "param", required = true) RoomType roomType) {
        CommonResultMap map = new CommonResultMap();
        try {
            projectAppartmentImpl.deletePrjStylePic(roomType);
            map.setResult(CommonResult.SUCCESS, "Create success");
        } catch (Exception e) {
            logger.error("deletePictures error.", e);
            map.setResult(CommonResult.ERROR, e.getMessage());
        }
        return Response.status(HttpCodeEnum.OK.getCode()).entity(map).build();
    }

    @POST
    @Path("/savePrjPicAndInfo")
    @Produces(MediaType.APPLICATION_JSON)
    @Consumes(MediaType.APPLICATION_JSON)
    @ApiOperation(value = "保存与编辑项目房型下的价格、设施、详细图片等信息")
    public Response savePrjPicAndInfomation(
            @ApiParam(value = "param", required = true) RoomType roomType) {
        CommonResultMap map = new CommonResultMap();
        try {
            // ProjectAppartment projectAppartment =
            // ProjectAppartmentDTO.toProjectApartmentPOJO(projectAppartmentDTO);
            Integer id = projectAppartmentImpl.savePrjPicAndInfo(roomType);
            map.setData(id);
            map.setResult(CommonResult.SUCCESS, "Create success");
        } catch (Exception e) {
            logger.error("savePrjPicAndInfomation error.", e);
            map.setResult(CommonResult.ERROR, e.getMessage());
        }
        return Response.status(HttpCodeEnum.OK.getCode()).entity(map).build();
    }


    @POST
    @Path("/RoomStylelist/{projectId}")
    @Produces(MediaType.APPLICATION_JSON)
    @Consumes(MediaType.APPLICATION_JSON)
    @ApiOperation(value = "根据项目id获取房型列表", position = 2)
    public Response RoomTypeList(@ApiParam(value = "param", required = true) @PathParam("projectId") Integer projectId,
                                 QueryUtil queryUtil) {
        CommonResultMap map = new CommonResultMap();
        List<RoomTypeDTO> roomTypeDTOList = null;
        try {
            queryUtil.getPagingTool().calculateStartIndex();
            roomTypeDTOList = (List<RoomTypeDTO>) projectAppartmentImpl.queryRoomTypeList(queryUtil, projectId);
            Integer totalCount = queryUtil.getPagingTool().getTotalNum();
            map.setData(roomTypeDTOList);
            map.setResultTotalCount(totalCount);
            map.setResult(CommonResult.SUCCESS);
        } catch (Exception e) {
            logger.error("RoomTypeList error.", e);
            map.setResult(CommonResult.ERROR, e.getMessage());
        }
        return Response.status(HttpCodeEnum.OK.getCode()).entity(map).build();
    }

    @POST
    @Path("/RoomStylelist")
    @Produces(MediaType.APPLICATION_JSON)
    @Consumes(MediaType.APPLICATION_JSON)
    @ApiOperation(value = "获取所有房型列表(价格、设备、详情图片)", position = 2)
    public Response allRoomTypeList(@ApiParam(value = "param", required = true)
                                            QueryUtil queryUtil) {
        CommonResultMap map = new CommonResultMap();
        List<RoomTypeDTO> roomTypeDTOList = null;
        try {
            queryUtil.getPagingTool().calculateStartIndex();
            roomTypeDTOList = (List<RoomTypeDTO>) projectAppartmentImpl.queryAllRoomTypeList(queryUtil);
            Integer totalCount = queryUtil.getPagingTool().getTotalNum();
            map.setData(roomTypeDTOList);
            map.setResultTotalCount(totalCount);
            map.setResult(CommonResult.SUCCESS);
        } catch (Exception e) {
            logger.error("allRoomTypeList error.", e);
            map.setResult(CommonResult.ERROR, e.getMessage());
        }
        return Response.status(HttpCodeEnum.OK.getCode()).entity(map).build();
    }

    @DELETE
    @Path("/deleteRoomType")
    @Produces(MediaType.APPLICATION_JSON)
    @ApiOperation(value = "根据id删除房型对应的价格、设备、详情图片（支持批量删除，id以逗号隔开）", position = 12)
    public Response deleteRoomType(
            @ApiParam(value = "param", required = true) @QueryParam("ids") String ids) {
        CommonResultMap map = new CommonResultMap();
        try {
            projectAppartmentImpl.deleteRoomType(ids);
            map.setResult(CommonResult.SUCCESS);
        } catch (Exception e) {
            logger.error("deleteRoomType error.", e);
            map.setResult(CommonResult.ERROR, e.getMessage());
        }
        return Response.status(HttpCodeEnum.OK.getCode()).entity(map).build();
    }

    @POST
    @Path("/roomPage")
    @Produces(MediaType.APPLICATION_JSON)
    @Consumes(MediaType.APPLICATION_JSON)
    @ApiOperation(value = "房态图-房间列表")
    public Response roomPage(
            @ApiParam(value = "param", required = true) RoomStatusListReq req) {
        CommonResultMap map = new CommonResultMap();
        try {
            map.setData(projectAppartmentImpl.queryRoomList(req));
            map.setResult(CommonResult.SUCCESS);
        } catch (Exception e) {
            logger.error("roomPage error.", e);
            map.setResult(CommonResult.ERROR, e.getMessage());
        }
        return Response.status(HttpCodeEnum.OK.getCode()).entity(map).build();
    }


    @POST
    @Path("/roomStatistic")
    @Produces(MediaType.APPLICATION_JSON)
    @Consumes(MediaType.APPLICATION_JSON)
    @ApiOperation(value = "房态图-房间统计")
    public Response roomStatistic(@ApiParam(value = "param", required = true) RoomStatusStatisticReq req) {
        CommonResultMap map = new CommonResultMap();
        try {
            map.setData(projectAppartmentImpl.roomStatistic(req));
            map.setResult(CommonResult.SUCCESS);
        } catch (Exception e) {
            logger.error("roomStatistic error.", e);
            map.setResult(CommonResult.ERROR, e.getMessage());
        }
        return Response.status(HttpCodeEnum.OK.getCode()).entity(map).build();
    }

    @GET
    @Path("/getAppendInfo/{id}")
    @Produces(MediaType.APPLICATION_JSON)
    @Consumes(MediaType.APPLICATION_JSON)
    @ApiOperation(value = "房态图-获取运营详情")
    public Response getAppendInfo(@ApiParam(value = "param", required = true) @PathParam("id") Integer id) {
        CommonResultMap map = new CommonResultMap();
        try {
            map.setData(projectAppartmentImpl.getAppendInfo(id));
            map.setResult(CommonResult.SUCCESS);
        } catch (Exception e) {
            logger.error("roomStatistic error.", e);
            map.setResult(CommonResult.ERROR, e.getMessage());
        }
        return Response.status(HttpCodeEnum.OK.getCode()).entity(map).build();
    }


    //删除项目图片信息
    @POST
    @Path("/test")
    @Produces(MediaType.APPLICATION_JSON)
    @Consumes(MediaType.APPLICATION_JSON)
    @ApiOperation(value = "根据项目id和房型，删除项目房型下的价格、设施、图片信息")
    public Response test() {
        CommonResultMap map = new CommonResultMap();
        try {
            projectAppartmentImpl.test();
            map.setResult(CommonResult.SUCCESS, "Create success");
        } catch (Exception e) {
            logger.error("test error.", e);
            map.setResult(CommonResult.ERROR, e.getMessage());
        }
        return Response.status(HttpCodeEnum.OK.getCode()).entity(map).build();
    }

    //编辑房间
    @POST
    @Path("/updateRoom")
    @Produces(MediaType.APPLICATION_JSON)
    @Consumes(MediaType.APPLICATION_JSON)
    @ApiOperation(value = "编辑房间基本属性")
    public Response updateRoom(
            @ApiParam(value = "param", required = true) Room room) {
        CommonResultMap map = new CommonResultMap();
        try {
            projectAppartmentImpl.update(room);
            map.setResult(CommonResult.SUCCESS, "Create success");
        } catch (BusinessException e) {
            logger.error("updateRoom error.", e);
            map.setResult(CommonResult.ERROR, e.getMessage());
        } catch (Exception e) {
            logger.error("updateRoom error.", e);
            map.setResult(CommonResult.ERROR, e.getMessage());
        }
        return Response.status(HttpCodeEnum.OK.getCode()).entity(map).build();
    }
}


